package com.shd.edanpei.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.Pair;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.citypicker.CityPickerActivity;
import com.shd.edanpei.map.MapManger;
import com.shd.edanpei.ui.activity.DynamicsActivity;
import com.shd.edanpei.ui.activity.DynamicsDetailsActivity;
import com.shd.edanpei.ui.activity.HomeSearchActivity;
import com.shd.edanpei.ui.adapter.HomeAdapter;
import com.shd.edanpei.base.BaseFragment;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.presenter.contract.HomeContract;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.RoleEnum;
import com.shd.edanpei.presenter.HomePresenter;
import com.shd.edanpei.ui.activity.TakeOrderDetailsActivity;
import com.shd.edanpei.ui.view.HomeFootView;
import com.shd.edanpei.ui.view.HomeHeadView;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

/**
 * Created by mc on 2018/9/18.
 * 首页
 */

@SuppressWarnings("ALL")
public class HomeFragment extends BaseFragment<HomePresenter> implements HomeContract.HomeIView {

//    @BindView(R.id.tv_city)
//    TextView tvCity;
//    @BindView(R.id.search)
//    View search;
//    @BindView(R.id.iv_search)
//    View iconSearch;
//    @BindView(R.id.tv_search)
//    View textSearch;

    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private HomeAdapter mAdapter;
    private int roleType;
    private HomeFootView homeFootView;
    private HomeHeadView homeHeadView;

    @Override
    protected Object setLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected HomePresenter bindPresenter() {
        return new HomePresenter(this);
    }

    @Override
    protected void initView(@Nullable View view, @Nullable Bundle savedInstanceState) {

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        homeFootView = new HomeFootView(getContext());
        mAdapter = new HomeAdapter(getContext());
         homeHeadView=new HomeHeadView(getContext());
        mAdapter.addHeaderView(homeHeadView);
        mAdapter.addFooterView(homeFootView);
        recyclerView.setAdapter(mAdapter);
        roleType = DaoManager.getInstance().getUserBean().getRoleType();
        if (roleType == RoleEnum.bank.getId()) {
            mPresenter.refresh();
        } else if (roleType == RoleEnum.other.getId()
                || roleType == RoleEnum.boss.getId()) {
            mPresenter.allRefresh();
        }


    }

//    private void initSearch() {
//        final Activity mActivity = (Activity) getContext();
//        search.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Pair<View, String> view1 = Pair.create(search, "search");
//                Pair<View, String> view2 = Pair.create(iconSearch, "icon_search");
//                Pair<View, String> view3 = Pair.create(textSearch, "text_search");
//                ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(mActivity, view1, view2, view3);
//                ActivityCompat.startActivity(mActivity, new Intent(mActivity, HomeSearchActivity.class), compat.toBundle());
//            }
//        });
//        MapManger.getLocation(this);
//    }


    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                homeHeadView.requestBannerData();
                if (roleType == RoleEnum.bank.getId()) {
                    mPresenter.refresh();
                } else if (roleType == RoleEnum.other.getId()
                        || roleType == RoleEnum.boss.getId()) {
                    mPresenter.allRefresh();
                } else {
                    refreshLayout.setRefreshing(false);
                }
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.ORDER_ID, mAdapter.getItem(position).getOrderId());
                bundle.putInt(Constant.INDEX, position);
                startActivity(TakeOrderDetailsActivity.class, bundle);
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_take:
                        //我要接单
                        if (mAdapter.getItem(position).getUserStatus() == 2) {
                            mPresenter.showTakeDialog(mAdapter.getItem(position).getOrderId(), position);
                        } else {
                            //已经被接单
                            ToastUtil.showToast("已被接单");
                        }

                        break;
                }
            }
        });
    }
    private static final int REQUEST_CODE_PICK_CITY = 0;

//    @OnClick(R.id.tv_city)
//    public void selectCity() {
//        startActivityForResult(new Intent(getContext(), CityPickerActivity.class),
//                REQUEST_CODE_PICK_CITY);
//    }
//
//    //返回选中城市
//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        if (requestCode == REQUEST_CODE_PICK_CITY && resultCode == RESULT_OK){
//            if (data != null){
//                String city = data.getStringExtra(CityPickerActivity.KEY_PICKED_CITY);
////                if (!city.equals("全国")) {
////                    city += "市";
////                }
//                tvCity.setText(city);
//            }
//        } else {
//
//        }
//    }

    @Override
    public HomeAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    @Override
    public void addFootView() {
        homeFootView.setLayout(1);
    }

    @Override
    public void removeFootView() {
        homeFootView.setLayout(2);
    }

//    @Override
//    public void onLocationSuccess(AMapLocation aMapLocation) {
//        String mCityName = aMapLocation.getCity();
//        tvCity.setText(mCityName);
//    }
//
//    @Override
//    public void onLocationFail(String error) {
//
//    }
}
